<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <div class='userInfo'>
        <img :src="userinfo.user_pic" alt="" class="avatar" v-if="userinfo.user_pic"/>
        <img src='../../assets/logo.png' alt="" class="avatar" v-else />
        <span>{{userinfo.nickname||userinfo.username}}</span>
        <span @click="clickLogout">退出</span>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <!-- 个人信息开始了 -->
        <el-aside width="200px">
          <div class="user-box">
            <img src="../../assets/logo.png" alt="" />
            <span>欢迎{{userinfo.nickname||userinfo.username}} </span>
          </div>
        </el-aside>
            <!-- 个人信息结束了 -->
            <!-- 侧边栏开始 -->
            <!--  default-active:默认高亮 -->

            <el-menu default-active="3" class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="pink"
            text-color="#fff"
            unique-opened
            active-text-color="#ffd04b">
                <!-- router -->
                <template v-for="value in initCathetObj" >
                    <el-menu-item :index="value.indexPath" :key="value.indexPath" v-if="!value.children">
                        <i class="el-icon-menu"></i>
                        <span slot="title">{{value.title}}</span>
                    </el-menu-item>
                    <el-submenu :index="value.indexPath" :key="value.icon" v-else>
                        <template slot="title">
                        <i :class="value.icon"></i>
                        <span>{{value.title}}</span>
                        </template>
                        <el-menu-item-group   v-for="item in value.children" :key="item.indexPath">
                        <el-menu-item :class="item.icon" :index="item.indexPath">{{item.title}}</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                </template>
            </el-menu>
          <!-- 侧边栏结束 -->
        </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          Main.vue后台主页
            {{initCathetObj}}
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'MainName',
  data () {
    return {
      initCathetObj: {}
    }
  },
  methods: {
    // 侧边导航
    handleOpen (key, keyPath) {
      // key为index
      // keyPath为数组
      console.log('1', key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log('2', key, keyPath)
    },
    //
    clickLogout () {
      this.$confirm('此操作将会退出登录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点击退出，清除token
        // 1.localStorage.setItem('vuex', '')
        // 2.localStorage.removeItem('vuex')
        this.$store.commit('undateToken', '')
        this.$message({
          type: 'success',
          message: '退出成功!'
        })

        this.$router.push('/login')
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消'
        // })ss
      })
    },
    initCathet () {
      console.log(this.token)
      this.http({
        url: '/my/menus',
        method: 'get',
        headers: { Authorization: this.token }
      }).then((res) => {
        // console.log(res)
        this.initCathetObj = res.data.data
      })
    }

  },
  created () {
    this.$store.dispatch('initUserinfo')
    console.log(this.$store.state.userinfo)
    this.initCathet()
  },
  computed: {
    ...mapState(['userinfo', 'token'])
  }

}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
    .userInfo {
      display: flex;
      align-items: center;
      span {
        margin:0 5px;
        color:white;
        font-size: 16px;
      }
    }
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #F2F2F2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
// 左侧边栏用户信息区域
.user-box {

  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
  // 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
}
</style>
